package com.jsmile.cloud.accountcenter.shop.model;

import java.util.Date;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.baomidou.mybatisplus.extension.activerecord.Model;
import com.jsmile.cloud.accountcenter.api.constants.AccountConstants;
import com.jsmile.cloud.accountcenter.api.req.shop.ReqTenantCreate;
import com.jsmile.cloud.accountcenter.api.req.shop.ReqTenantUpdate;
import com.jsmile.cloud.accountcenter.shop.event.tenant.TenantChangeNameEvent;
import com.jsmile.cloud.accountcenter.shop.event.tenant.TenantChangePhoneEvent;
import com.jsmile.cloud.accountcenter.shop.event.tenant.TenantRegisterEvent;
import com.jsmile.cloud.authc.api.enums.ClientSourceEnum;
import com.jsmile.cloud.authc.api.req.ReqSysUserRegister;
import com.jsmile.mail.security.event.DomainEventPublisher;
import com.jsmile.mall.api.enums.UserTypeEnum;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.IdUtil;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

/**
 * @author 龚亮
 * @version 1.0
 * @desc 为了薪资破万，还是睡一会儿吧...!
 * @date 2020年06月04日 04:39:24
 */
@ApiModel
@Data
@TableName("t_ac_tenant")
public class Tenant extends Model<Tenant> {

    private static final long serialVersionUID = 1L;

    @ApiModelProperty(value = "租户id")
    @TableId(value = "id", type = IdType.INPUT)
    private Long id;
    @ApiModelProperty(value = "租户编号")
    private String tenantNo;
    @ApiModelProperty(value = "租户名称")
    private String tenantName;
    @ApiModelProperty(value = "状态标识1启用 0禁用")
    private Integer enable;
    @ApiModelProperty(value = "联系人")
    private String contactName;
    @ApiModelProperty(value = "联系电话")
    private String contactPhone;
    @ApiModelProperty(value = "固定电话")
    private String telephone;
    @ApiModelProperty(value = "电子邮箱")
    private String email;
    @ApiModelProperty(value = "身份证")
    private String idCard;
    @ApiModelProperty(value = "省")
    private String province;
    @ApiModelProperty(value = "市")
    private String city;
    @ApiModelProperty(value = "区")
    private String district;
    @ApiModelProperty(value = "区编码")
    private String areaCode;
    @ApiModelProperty(value = "详细地址")
    private String detailAddress;
    @ApiModelProperty(value = "身份证照片")
    private String idCardImage;
    @ApiModelProperty(value = "创建时间")
    private Date createTime;
    @ApiModelProperty(value = "修改时间")
    private Date updateTime;
    @ApiModelProperty(value = "0:正常，1:删除")
    private Integer delFlag;
    @ApiModelProperty(value = "创建者")
    private String createBy;
    @ApiModelProperty(value = "修改者")
    private String updateBy;
    @ApiModelProperty(value = "商户地址")
    private String merchantUrl;
    @ApiModelProperty(value = "平台id")
    private Integer platformId;
    @ApiModelProperty(value = "业务id")
    private Integer bizCode;

    public static Tenant create(ReqTenantCreate reqTenantSave) {
        Tenant tenant = BeanUtil.toBean(reqTenantSave, Tenant.class);
        tenant.setCreateTime(new Date());
        tenant.setId(IdUtil.getSnowflake(AccountConstants.SNOWFLAKE.workerId, AccountConstants.SNOWFLAKE.datacenterId).nextId());
        DomainEventPublisher.publish(new TenantRegisterEvent(tenant));
        return tenant;
    }

    public ReqSysUserRegister registerUser() {
        ReqSysUserRegister sysUserRegisterTo = new ReqSysUserRegister();
        sysUserRegisterTo.setTenantId(this.getId());
        sysUserRegisterTo.setBizId(this.getId());
        sysUserRegisterTo.setUserType(UserTypeEnum.MERCHANT_USER.getValue());
        sysUserRegisterTo.setUserName(this.getTenantName());
        sysUserRegisterTo.setRealName(this.getContactName());
        sysUserRegisterTo.setNickname(this.getContactName());
        sysUserRegisterTo.setClientSource(ClientSourceEnum.MERCHANT.getValue());
        return sysUserRegisterTo;
    }

    public void updateTenant(ReqTenantUpdate reqTenantUpdate) {
        this.setUpdateTime(new Date());
        this.setProvince(reqTenantUpdate.getProvince());
        this.setTenantName(reqTenantUpdate.getTenantName());
        this.setCity(reqTenantUpdate.getCity());
        this.setDistrict(reqTenantUpdate.getDistrict());
        this.setDetailAddress(reqTenantUpdate.getDetailAddress());
        this.setContactName(reqTenantUpdate.getContactName());
        this.setContactPhone(reqTenantUpdate.getContactPhone());
        this.setTelephone(reqTenantUpdate.getTelephone());
        this.setEmail(reqTenantUpdate.getEmail());
        if (!this.getTenantName().equals(reqTenantUpdate.getTenantName())) {
            DomainEventPublisher.publish(new TenantChangeNameEvent(this));
        }
        if (!this.getContactPhone().equals(reqTenantUpdate.getContactPhone())) {
            DomainEventPublisher.publish(new TenantChangePhoneEvent(this));
        }
    }
}
